package cn.zsb.zsb.ctrl;

import cn.zsb.zsb.entity.Suser;
import cn.zsb.zsb.service.SuserService;
import cn.zsb.zsb.util.Md5Utils;
import cn.zsb.zsb.vo.Page;
import cn.zsb.zsb.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

//import javax.servlet.ServletException;
//import javax.servlet.ServletInputStream;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author oldliu
 * @since 1.0
 */
@RestController
@RequestMapping("suser")
@Api("学生操作")
public class SuserController {
    @Autowired
    private SuserService suserService;

    @GetMapping("search")
    @ApiOperation("查询分页操作")
    public Page<Suser> users(@ApiParam("页码，默认是1") @RequestParam(name = "page", defaultValue = "1") int page,
                             @ApiParam("页大小，默认是10") @RequestParam(name = "limit", defaultValue = "10") int limit,
                             @ApiParam("学生姓名") @RequestParam(name = "name", required = false) String name,
                             @ApiParam("学生手机号")  @RequestParam(name = "phone", required = false) String phone,
                             @ApiParam("学生学校")  @RequestParam(name = "school", required = false) String school,
                             @ApiParam("专业")  @RequestParam(name = "major", required = false) String major) {

        Page<Suser> search = suserService.search(phone, name, school, major, page, limit);
        return search;
    }



    @GetMapping("get")
    public Result get(long id) {
        Suser suser = this.suserService.get(id);
        if (suser != null)
            return Result.success(suser);
        return Result.error("对象不存在");
    }
//    @GetMapping("get2/{id}")
//    public Result get2(@PathVariable long id) {
//        Suser suser = this.suserService.get(id);
//        if (suser != null)
//            return Result.success(suser);
//        return Result.error("对象不存在");
//    }
    @DeleteMapping("del/{id}")//del?id=100
    public Result del(@PathVariable("id") long id) {//
       int r=this.suserService.batDel(new long[]{id});
       if(r>0)
           return  Result.success("删除成功");
        return Result.error("对象不存在");
    }
    @PostMapping("add")
    public Result add(@Validated(Suser.Add.class)@RequestBody Suser user)  {
        //,HttpServletRequest request
        //ServletInputStream in = request.getInputStream();
        //BufferedReader br=new BufferedReader(new InputStreamReader(in));
        //String str=null;
        //输出的格式，类似{"sname":"施瓦辛格","phone":"13211111111","school":"安庆师范","major":"数学","pass":"123456789","sex":"男",
        // "isgraduate":"true","type":"专科"}
//        while ((str=br.readLine())!=null){
//            System.out.println(str);
//        }

        user.setPass(Md5Utils.hash(user.getPass()));
        int add = this.suserService.add(user);
        return Result.success();
    }
    @PostMapping("update")
    public Result update(@Validated(Suser.Update.class)@RequestBody Suser user) {

        int add = this.suserService.update(user);
        return Result.success();
    }
//    @PutMapping("update")
//    public Result update2(@Validated(Suser.Update.class)@RequestBody Suser user) {
//
//        int add = this.suserService.update(user);
//        return Result.success();
//    }
    @PostMapping("resetPass")
    public Result resetPass(@Validated(Suser.UpdatePass.class) Suser user) {
        user.setPass(Md5Utils.hash(user.getPass()));
        int add = this.suserService.resetPass(user);
        return Result.success();
    }
}
